#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <libintl.h>
#include <locale.h>
#include <glib.h>

#define _(string) gettext(string)

char *remove_space(char * str){
  char * tail = str;
  char * next = str;

  while(*next){
    if(*next != ' '){
      if(tail < next)
        *tail = *next;
      tail++;
    }
    next++;
  }
  *tail = '\0';
  return str;
}

int datecompare(char *termdate){
  int yy,mm,dd;
  char c;
  struct tm t={0};
  time_t system_time;
  time_t term_time;
  sscanf(termdate,"%4d%c%2d%c%2d",&yy,&c,&mm,&c,&dd);
 // printf("%4d  %2d  %2d",yy,mm,dd);
  t.tm_year=yy-1900;
  t.tm_mon=mm-1;
  t.tm_mday=dd;
  time(&system_time);
  term_time=mktime(&t);
  if(system_time>term_time){
  return -1;
  }else if(term_time-system_time<7948800){
  return 1+(term_time-system_time)/86400;
  }else{
  return 0;
  }
//debug
  //printf("now time:%ld\n",system_time);
  //printf("file time:%ld\n",term_time);
}

int write_term_status(char term_status){
  FILE *fp;
  char *statusfile;
  
  const char* homepath = g_get_home_dir();
  if(homepath==NULL||0 == strlen(homepath)){
     return -1;
  }
  statusfile = malloc(strlen(homepath)+strlen("/.config/k_status")+1);
  if (statusfile == NULL) return 2;  
  strcpy(statusfile, homepath);  
  strcat(statusfile, "/.config/k_status");  

  fp=fopen(statusfile,"w");
  if(fp==NULL){
  	 free(statusfile);
     return 1;
  }
  fputc(term_status,fp);
  fclose(fp);
  free(statusfile);
  return 0;
}

int compare_try_time_and_activation(char *activetime)
{
	FILE *fp = NULL;
	char buf[128+1] = {0};
	char *pPtr = NULL;
	int yy = 0;
	int mm = 0;
	int dd = 0;
  	char c;
  	struct tm t={0};
	time_t try_time = 0;
	time_t active_time = 0;
	
	fp = fopen("/etc/LICENSE", "r");
  	if(fp == NULL)
	{
    	//printf("fopen LICENSE error");
    	return 0;
  	}
	
	while(fgets(buf,128, fp))
	{
		pPtr = strstr(buf, "TERM");
    	if(pPtr != NULL)
		{
			pPtr += strlen("TERM:");
			sscanf(pPtr,"%4d%c%2d%c%2d",&yy,&c,&mm,&c,&dd);
  			//printf("try_time %4d  %2d  %2d",yy,mm,dd);
  			t.tm_year=yy-1900;
  			t.tm_mon=mm-1;
  			t.tm_mday=dd;
  			try_time=mktime(&t);
      		break;
    	}
	}
	fclose(fp);
	
	if(0 != try_time)
	{
		yy = 0;
		mm = 0;
	    dd = 0;
		sscanf(activetime,"%4d%c%2d%c%2d",&yy,&c,&mm,&c,&dd);
		//printf("activetime %4d  %2d  %2d",yy,mm,dd);
		t.tm_year=yy-1900;
		t.tm_mon=mm-1;
		t.tm_mday=dd;
		active_time=mktime(&t);
		if(active_time > try_time)
		{
			return 1;
		}
	}
	
	return 0;
}

int main(){
  setlocale(LC_ALL,"");
  bindtextdomain("kylin-verify","/usr/share/locale");
  bind_textdomain_codeset("kylin-verify","UTF-8");
  textdomain("kylin-verify");

//  FILE *fp;
  int status=0;
  int term_status=0;
  int write_status=0;
  int bActive=0;
//  char buf[128+1];
  char *value_term=NULL;
  char *value_key=NULL;
//  char *str, *key, *value_s;
  char *error_str=_("Unable to determine your version information.\n");
     
/*  fp = fopen("/etc/.kyinfo", "r");
  if(fp == NULL){
    write_status=write_term_status('e');
    printf("%s", error_str);
    return -1;
  }*/


  GKeyFile* config = g_key_file_new();
  if(!g_key_file_load_from_file(config,"/etc/.kyinfo",G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS,NULL))
  {
    g_key_file_free(config);
    write_status=write_term_status('e');
    printf("%s", error_str);
    return -1;

  }
     
  value_key = g_key_file_get_value(config,"servicekey","key",NULL);
  if(value_key == NULL || strlen(value_key) == 0 || strcmp(value_key, "") == 0)
  {
        g_key_file_free(config);
        write_status=write_term_status('i');
        printf("%s",  _("To obtain service serial number error!\n"));
        return -1;

  }

  value_term = g_key_file_get_value(config,"os","term",NULL);
  if(value_term == NULL || strlen(value_term) == 0 || strcmp(value_term, "") == 0)
  {
	g_key_file_free(config);
	write_status=write_term_status('n');
        printf("%s", _("Access to service cycle information error!\n"));
        return -1;
  }

  g_key_file_free(config);
  
/*  while(fgets(buf,128, fp)){
    if(strstr(buf, "#") != NULL){
      continue;
    }
     
    str = (char *)remove_space((unsigned char *)buf);
    key = strtok(str, "=");
    if(strcasecmp(key, "term") == 0){
      value_s=strtok(NULL, "=");
      if((!value_s) || (strcmp(value_s ,"\r") == 0) || (strcmp(value_s, "\n") == 0)){
	fclose(fp);
	write_status=write_term_status('i');
        printf("%s", _("Access to service cycle information error!\n"));
        return -1;
      }
      strcpy(value_term,strtok(value_s, "\n"));
      status++;
    }else if(strcasecmp(key, "key") == 0){
	 value_s=strtok(NULL, "=");
      if((!value_s) || (strcmp(value_s ,"\r") == 0) || (strcmp(value_s, "\n") == 0)){
        fclose(fp);
	write_status=write_term_status('n');
        printf("%s",  _("To obtain service serial number error!\n"));
        return -1;
      }
      strcpy(value_key,value_s);
      status++;
	}
  }
  fclose(fp);*/

  bActive = compare_try_time_and_activation(value_term);
  if(1 == bActive)
  {
  	write_status=write_term_status('a');
  	return 0;
  }

  term_status=datecompare(value_term);
  
  //printf("到期状态：%d\n",term_status);
  
/*  if(status==0){
  printf("%s", error_str);
  return -1;
   }else{ */
	if(term_status==-1){
        write_status=write_term_status('s');
  	printf(_("Your service cycle has over!\n"));
  	}else if(term_status>0){
        write_status=write_term_status('q');
	printf(_("Your service cycle had less than %d day!\n"),term_status);
	}else{
        write_status=write_term_status('o');
        }
   printf(_("Your service cycle by:%s,SN:%s"), value_term,value_key);
   return 0;
//   }
}
